package com.zjsystem.zjsy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.zjsystem.zjsy.dto.*;
import com.zjsystem.zjsy.enums.AuditEnums;
import com.zjsystem.zjsy.enums.PlanDwEnums;
import com.zjsystem.zjsy.enums.RegulateTypeEnums;
import com.zjsystem.zjsy.enums.SubStatusEnums;
import com.zjsystem.zjsy.result.Result;
import com.zjsystem.zjsy.service.*;
import com.zjsystem.zjsy.utils.DateTimes;
import com.zjsystem.zjsy.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 王云飞
 * @name: MoNiChouHuaController
 * @Date: 2024/7/2 15:46
 */
@Slf4j
@RestController
@RequestMapping("/zjsy")
public class MoNiChouHuaController {
    @Resource
    private ZjxdPlanService planService;
    @Resource
    private ZjxdCehuaSchemeService cehuaSchemeService;
    @Resource
    private ZjxdPlanSchemeService planSchemeService;
    @Resource
    private TempTableService tempTebleService;
    @Resource
    private InstructPreviewService previewService;


    @GetMapping("/testSave")
    @Transactional
    public Result testSave(@RequestParam("type") String type, @RequestParam("id") String id) {
        try {
            if (type.equals(AuditEnums.CHOUHUAZ.name())){
                 return chouSave(id);
            } else {
                return ceSave(id);
            }
        } catch (Exception e) {
            log.error("模拟筹划系统新增报错 ：{}", e.getMessage());
            throw new RuntimeException("模拟筹划系统新增报错 请联系开发人员");
        }
    }

    public Result chouSave(String id) {
        ZjxdPlan zjxdPlan = planService.getOne(Wrappers.<ZjxdPlan>lambdaQuery().eq(ZjxdPlan::getAssId, id));
        List<ZjxdPlanScheme> chouList = planSchemeService.list(Wrappers.<ZjxdPlanScheme>lambdaQuery().eq(ZjxdPlanScheme::getPlanId, zjxdPlan.getId()));
        if (CollectionUtils.isNotEmpty(chouList)) {
            List<ZjxdPlanScheme> collect = chouList.stream().filter(c ->
                    c.getSubStatus().equals(SubStatusEnums.DSH.name()) ||
                            c.getSubStatus().equals(SubStatusEnums.TG.name())
            ).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                return Result.fail().message("还有一条未审核记录，请审核完成之后再推送方案，谢谢");
            }
        }

        ZjxdPlanScheme chou = new ZjxdPlanScheme();
        String subValue = "预热造势”蔡英文私带烟议题”，计划2024.8.1-2024.8.3，打击蔡英文公私不分，滥用职权，受众目标为台蓝白以及浅绿民众，投送通道为电视台名嘴讨论，行动类型为特定信息";
        String schemeName = "方案是塑造台民众的不信任感";

        chou.setPlanId(zjxdPlan.getId());
        chou.setSubDw(PlanDwEnums.CHOUHUATWO.name());
        chou.setSubValue(subValue);
        chou.setSubTime(DateTimes.now());
        chou.setSubStatus(SubStatusEnums.DSH.name());
        chou.setSchemeName(schemeName);
        planSchemeService.save(chou);
        log.info("筹划新增数据：{}", JSON.toJSONString(chou));
        TempTable tempTeble = new TempTable();
        tempTeble.setGlId(chou.getId());
        tempTeble.setFamc("塑造台民众的不信任感");
        tempTeble.setXdmc("引爆热点“蔡英文记者会言论失格");
        tempTeble.setXdlx("外线策应发声");
        tempTeble.setStartTime("2024-8-2 12:00:00");
        tempTeble.setEndTime("2024-8-4 24:59:59");
        tempTeble.setZjmb("蔡智囊团与蔡不合");
        tempTeble.setSzmb("蓝白以及浅绿民众");
        tempTeble.setTstd("PTT大V转发评论");
        tempTeble.setType(AuditEnums.CHOUHUAZ.name());
        tempTeble.setUpdateTime(new Date());
        tempTebleService.save(tempTeble);
        log.info("筹划 -- 【行动详情】 新增数据：{}", JSON.toJSONString(tempTeble));

        savePreview(chou.getId(), chou.getSubValue(), tempTeble.getTstd());

        return Result.ok().message("新增筹划成功");
    }

    public Result<Object> ceSave(String id) {
        ZjxdPlan zjxdPlan = planService.getOne(Wrappers.<ZjxdPlan>lambdaQuery().eq(ZjxdPlan::getAssId, id));
        List<ZjxdCehuaScheme> ce = cehuaSchemeService.list(Wrappers.<ZjxdCehuaScheme>lambdaQuery().eq(ZjxdCehuaScheme::getPlanId, zjxdPlan.getId()));
        if (CollectionUtils.isNotEmpty(ce)){
            List<ZjxdCehuaScheme> collect = ce.stream().filter(c ->
                    c.getSubStatus().equals(SubStatusEnums.DSH.name()) ||
                            c.getSubStatus().equals(SubStatusEnums.TG.name())
            ).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)){
                return Result.fail().message("还有一条未审核记录，请审核完成之后再推送方案，谢谢");
            }
        }
        ZjxdCehuaScheme cehuaScheme = new ZjxdCehuaScheme();
        String subValue = "引爆热点“蔡英文记者会言论失格”，计划-2024.8.4，打击蔡智囊团与蔡不合，受众目标为台蓝白以及浅绿民众，投送通道为PTT大V转发评论，行动类型为外线策应发声";
        String schemeName = "方案是塑造台民众的不信任感";
        cehuaScheme.setPlanId(zjxdPlan.getId());
        cehuaScheme.setSubDw(PlanDwEnums.CHOUHUATWO.name());
        cehuaScheme.setSubValue(subValue);
        cehuaScheme.setSubTime(DateTimes.now());
        cehuaScheme.setSubStatus(SubStatusEnums.DSH.name());
        cehuaScheme.setSchemeName(schemeName);
        cehuaSchemeService.save(cehuaScheme);
        log.info("策划新增数据：{}", JSON.toJSONString(cehuaScheme));
        TempTable tempTeble = new TempTable();
        tempTeble.setGlId(cehuaScheme.getId());
        tempTeble.setFamc("塑造台民众的不信任感");
        tempTeble.setXdmc("引爆热点“蔡英文记者会言论失格");
        tempTeble.setXdlx("外线策应发声");
        tempTeble.setStartTime("2024-8-2 12:00:00");
        tempTeble.setEndTime("2024-8-4 24:59:59");
        tempTeble.setZjmb("蔡智囊团与蔡不合");
        tempTeble.setSzmb("蓝白以及浅绿民众");
        tempTeble.setTstd("PTT大V转发评论");
        tempTeble.setType(AuditEnums.CEHAZ.name());
        tempTebleService.save(tempTeble);
        savePreview(cehuaScheme.getId(), cehuaScheme.getSubValue(), tempTeble.getTstd());
        log.info("策划 -- 【行动详情】 新增数据：{}", JSON.toJSONString(tempTeble));
        return Result.ok().message("新增策划成功");
    }

    public void savePreview(Long id, String fcmc, String tstd){
        List<InstructPreview> previewList = Lists.newArrayList();
        InstructPreview previewOne = new InstructPreview();
        previewOne.setTableId(id);
        previewOne.setCommandValue("指挥中心 调控岗位 对" + fcmc + "通过" + tstd + "发布");
        previewOne.setCommandType(RegulateTypeEnums.SPY.getDesc());
        previewOne.setDutyDept("筹划1岗位， 筹划2岗位");
        previewList.add(previewOne);
        InstructPreview previewTwo = new InstructPreview();
        previewTwo.setTableId(id);
        previewTwo.setCommandValue("指挥中心 调控岗位 对" + fcmc + "通过Facebook发布");
        previewTwo.setCommandType(RegulateTypeEnums.SPY.getDesc());
        previewTwo.setDutyDept("筹划1岗位， 筹划2岗位");
        previewList.add(previewOne);
        previewService.saveBatch(previewList);
    }

    @GetMapping("/getTableInfo")
    public Result<Object> getTableInfo(@RequestParam("id") String id){
        TempTable one = tempTebleService.getById(id);
        return Result.ok(one);
    }
}
